package com.arpost.pages;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

public class ShareUtils {

	private static final String TAG = "SHARE_UTILS";

	private Activity mActivity;

	public ShareUtils(Activity rootActivity) {
		mActivity = rootActivity;
	}

	public void shareToWXFriend(String text, File file) {
		Intent intent = new Intent();
		ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
		intent.setComponent(comp);
		intent.setAction("android.intent.action.SEND");
		intent.setType("image/*");
		intent.putExtra(Intent.EXTRA_TEXT, text);
		intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
		mActivity.startActivity(intent);
	}

	public void shareToWXTimeLine(String text, Uri uri) {
		Intent intent = new Intent();
		ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
		intent.setComponent(comp);
		intent.setAction("android.intent.action.SEND");
		intent.setType("image/*");
		intent.putExtra("Kdescription", "#龟兔再跑大电影#");
		intent.putExtra(Intent.EXTRA_STREAM, uri);
		mActivity.startActivity(intent);
	}

	public void shareToWXTimeLine(String text, File file) {
		shareToWXTimeLine(text, Uri.fromFile(file));
	}

	public void shareToWB(String text, Uri uri) {
		Intent intent = new Intent("android.intent.action.SEND");
		intent.setType("image/*");
		intent.putExtra(Intent.EXTRA_TEXT, "#龟兔再跑大电影#");
		intent.putExtra(Intent.EXTRA_STREAM, uri);

		PackageManager pm = mActivity.getApplicationContext().getPackageManager();
		List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0);
		for (ResolveInfo app : activityList) {
			Log.i(TAG, app.activityInfo.name);
			if ((app.activityInfo.name).contains("weibo")) {
				ActivityInfo activity = app.activityInfo;
				ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
				// intent.addCategory(Intent.CATEGORY_LAUNCHER);
				// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
				// Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				intent.setComponent(name);
				mActivity.startActivity(intent);
				break;
			}
		}
	}

	public void shareToWB(String text, File file) {
		shareToWB(text, Uri.fromFile(file));
	}

	public static File saveBitmap(Bitmap bmp, String fileName) {
		try {
			// mkdir
			String path = Environment.getExternalStorageDirectory().toString() + "/龟兔再跑大电影";
			File dir = new File(path);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			File f = new File(path, fileName);
			String full_path = path + "/" + fileName;
			Log.i(TAG, "bitmap stored to " + full_path);
			FileOutputStream fOut = new FileOutputStream(f);

			bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
			fOut.flush();
			fOut.close();

			return f;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static File getBitmap(String filename) {
		try {
			String path = Environment.getExternalStorageDirectory().toString() + "/tr";
			File f = new File(path, filename);
			return f;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	


}
